﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using JaxDug.Areas.CodeCamp.Models;
using JaxDug.DataAccess;

namespace JaxDug.Areas.CodeCamp.Controllers
{
    public partial class RegistrationController : Controller
    {
        //
        // GET: /CodeCamp/Registration/

        public virtual ActionResult Index()
        {
            MembershipUser myObject = Membership.GetUser();
            Guid UserID = (Guid)myObject.ProviderUserKey;
            JaxdugEntities db = new JaxdugEntities();
            var matchingUser = db.Speakers.Where(m => m.UserID == UserID);
            if (matchingUser.Any())
            {
                ViewBag.AlreadyRegistered = true;
            }
            else
            {
                ViewBag.AlreadyRegistered = false;
            }
            return View();
        }

        [HttpPost]
        public virtual ActionResult Index(Registration model)
        {
            MembershipUser myObject = Membership.GetUser();
            Guid UserID = (Guid)myObject.ProviderUserKey;
            JaxdugEntities db = new JaxdugEntities();
            if (!db.Speakers.Where(m => m.UserID == UserID).Any())
            {
                Speaker _speaker = new Speaker();
                _speaker.UserID = UserID;
                _speaker.FirstName = model.FirstName;
                _speaker.LastName = model.LastName;
                _speaker.Phone = model.Phone;
                db.Speakers.AddObject(_speaker);
                if (!db.Profiles.Where(m => m.UserID == UserID).Any())
                {
                    Profile _profile = new Profile();
                    //_profile.Speaker = _speaker;
                    _profile.UserID = UserID;
                    _profile.Company = model.CompanyName;
                    _profile.Bio = model.Bio;
                    _profile.Twitter = model.Twitter;
                    db.Profiles.AddObject(_profile);
                }
            }

            db.SaveChanges();
            ViewBag.AlreadyRegistered = true;
            return View("ThankYouForRegistering", model);
        }
    }
}
